<?php

namespace App\Jobs;

use App\Models\Resume;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;


class ResumeJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public Resume $resume;

    public function __construct(Resume $resume)
    {
        $this->resume = $resume;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
 
         $arr = ['资金专员','资金经理','资金高级经理','资金主管','资金总监','融资资金专员','融资资金高级经理','融资资金主管','融资资金经理','融资资金总监','资金结算专员','资金结算高级经理','资金结算主管','资金结算经理','资金结算总监','税务专员','税务经理','税务高级经理','税务主管','税务总监','会计','总账会计','核算专员','核算经理','核算总监','BP','FPA','预算预测','经分','渠道','市场零售','营销','售后','服务','交付','物流','内控专员','内控经理','内控高级经理','内控主管','内控总监','审计专员','审计经理','审计高级经理','审计主管','审计经理','审计总监','内审专员','内审经理','内审高级经理','内审总监','内审主管'];

        $certificateAcrArr = ['CPA','注册会计师','AICPA','CICPA','HICPA','ACCA','特许会计师']; 

        $zzykeywordArr = ['制造','生产','机械','自动化','工程','工艺','供应链','产品开发','质量控制','材料科学','精益生产','设备维护'];
        $zzyCompanyArr = ['通用电气','GE','西门子','Siemens','波音','Boeing','霍尼韦尔','Honeywell','施耐德电气','Schneider Electric','ABB','洛克希德·马丁','Lockheed Martin','卡特彼勒','Caterpillar','三菱重工','Mitsubishi Heavy Industries','飞利浦','Philips'];

        $qcKeywordArr = ['汽车','车辆','引擎','动力系统','汽车设计','汽车制造','汽车销售','汽车维修','电动汽车','混合动力','自动驾驶','汽车配件'];
        $qcCompanyArr = ['丰田','Toyota','宝马','BMW','福特','Ford','特斯拉','Tesla','本田','Honda','奔驰','Mercedes-Benz','奥迪','Audi','大众','Volkswagen','雪佛兰','Chevrolet','现代','Hyundai'];

        $hlwKeywordArr = ['互联网','网络','网站','软件开发','编程','数据科学','数据分析','云计算','人工智能','机器学习','网络安全','移动应用','前端开发','后端开发'];
        $hlwCompanyArr = ['谷歌','Google','亚马逊','Amazon','腾讯','Tencent','阿里巴巴','Alibaba','百度','Baidu','Facebook','微软','Microsoft','苹果','Apple','甲骨文','Oracle','IBM','Adobe'];

        $gcKeywordArr = ['工厂','车间','生产线','操作工','工艺流程','质量控制','工业自动化','生产效率','生产管理','工业机器人','制造执行系统','MES','精益制造'];
        $gcCompanyArr = ['富士康','Foxconn','宝钢集团','Baosteel Group','英特尔','Intel','三星电子','Samsung Electronics','LG电子','LG Electronics','松下','Panasonic','索尼','Sony','海尔','Haier','美的集团','Midea Group'];

        $xlsKeywordArr = ['新零售','电子商务','线上销售','线下体验','物流配送','客户服务','市场分析','消费者行为','O2O','供应链管理','库存管理','数字营销','客户关系管理','CRM'];

        $xlsCompanyArr = ['盒马鲜生','京东','JD.com','京东到家','亚马逊','Amazon','Amazon Go','沃尔玛','Walmart','沃尔玛在线购物','好市多','Costco','拼多多','Pinduoduo','美团','Meituan','饿了么','Ele.me','苏宁易购','Suning.com'];
        $ysCompanyArr = ['华为','荣耀','oppo','vivo','中兴','蔚来','理想','小鹏'];

        $updateData = [];
        $postArr = [];
        $industryArr = [];
        $certificateArr= [];
        $ysArr = [];

        foreach($ysCompanyArr as $zzy){
             if(strpos($this->resume->content,$zzy)){
                $ysArr[] = '友商-关键词【'.$zzy.'】';
            }

        }
        foreach($arr as $pds){


            if(strpos($this->resume->content,$pds)){
                $postArr[] = $pds;
            }


        }

        foreach($certificateAcrArr as $crs){


            if(strpos($this->resume->content,$crs)){
                $certificateArr[] = $crs;
            }


        }
        foreach($zzykeywordArr as $zzy){
             if(strpos($this->resume->content,$zzy)){
                $industryArr[] = '制造业-关键词【'.$zzy.'】';
            }

        }
        foreach($zzyCompanyArr as $zzy){
             if(strpos($this->resume->content,$zzy)){
                $industryArr[] = '制造业-公司【'.$zzy.'】';
            }

        }

        foreach($qcKeywordArr as $qc){
             if(strpos($this->resume->content,$qc)){
                $industryArr[] = '汽车行业-关键词【'.$qc.'】';
            }

        }
        foreach($qcCompanyArr as $qc){
             if(strpos($this->resume->content,$qc)){
                $industryArr[] = '汽车行业-公司【'.$qc.'】';
            }

        }


        foreach($hlwKeywordArr as $hlw){
             if(strpos($this->resume->content,$hlw)){
                $industryArr[] = '互联网-关键词【'.$hlw.'】';
            }

        }
        foreach($hlwCompanyArr as $hlw){
             if(strpos($this->resume->content,$hlw)){
                $industryArr[] = '互联网-公司【'.$hlw.'】';
            }

        }

         foreach($gcKeywordArr as $gc){
             if(strpos($this->resume->content,$gc)){
                $industryArr[] = '工厂-关键词【'.$gc.'】';
            }

        }
        foreach($gcCompanyArr as $gc){
             if(strpos($this->resume->content,$gc)){
                $industryArr[] = '工厂-公司【'.$gc.'】';
            }

        }

        foreach($xlsKeywordArr as $xls){
             if(strpos($this->resume->content,$xls)){
                $industryArr[] = '新零售-关键词【'.$xls.'】';
            }

        }
        foreach($xlsCompanyArr as $xls){
             if(strpos($this->resume->content,$xls)){
                $industryArr[] = '新零售-公司【'.$xls.'】';
            }

        }

        if(!empty($postArr)){

            $updateData['post'] = implode(",",$postArr);
        }

          if(!empty($certificateArr)){

            $updateData['certificate'] = implode(",",$certificateArr);
        }

         if(!empty($industryArr)){

            $updateData['industry'] = implode(",",$industryArr);
        }

          if(!empty($ysArr)){

            $updateData['ys'] = implode(",",$ysArr);
        }

        $content = [
            'model' => 'gpt-3.5-turbo',
            'messages' => [
                ['role' => 'system', "content"=> "你是个人事经理"],
                ['role' => 'user', 'content' => '根据简历内容【'.$this->resume->content.'】判断候选者是否有过管理团队经历，管理的团队规模为多少,判断候选者是否有海外工作经验,要求在海外常驻过或者外派过至少半年才算是有海外工作经验，海外工作的年限多长，返回格式用json格式返回四个字段，分别是是否带过团队，团队规模,是否有海外工作经验,海外工作年限'],
            ],
        ];

        $content = $this->curl($content);
        
        $content = json_decode($content,true);

        $gptContent = $content['choices'][0]['message']['content']; 
        
        $gptArr = json_decode($gptContent,true);

        if( isset($gptArr['是否带过团队'] )){

            if($gptArr['是否带过团队'] ==true || $gptArr['是否带过团队'] =="是"){

                 $updateData['has_team'] = "是";

             }else{

                 $updateData['has_team'] = "否";
             }
        }else{      

              $updateData['has_team'] = "否";
        }

        if(isset($gptArr['团队规模'])){
            //$updateData['scale'] = $gptArr['团队规模'];
            $pattern = '/\d+/';
            preg_match_all($pattern, $gptArr['团队规模'], $matches);
            $numbers = $matches[0];

            if($numbers){
                $updateData['scale'] = max($numbers);

            }else{
                $updateData['scale'] = "无";

            }

        }else{

             $updateData['scale'] = "无";
        }

         if( isset($gptArr['是否有海外工作经验'] )){

            if($gptArr['是否有海外工作经验'] ==true || $gptArr['是否有海外工作经验'] =="是"){

                 $updateData['has_overseas_experience'] = "是";

             }else{

                 $updateData['has_overseas_experience'] = "否";
             }
        }else{      

              $updateData['has_overseas_experience'] = "否";
        }

        if(isset($gptArr['海外工作年限'])){
            //$updateData['overseas_seniority'] = $gptArr['海外工作年限'];

            $pattern = '/\d+/';
            preg_match_all($pattern, $gptArr['海外工作年限'], $matches);
            $numbers = $matches[0];

            if($numbers){
                $updateData['overseas_seniority'] = max($numbers);

            }else{
                $updateData['overseas_seniority'] = "无";

            }

        }else{

             $updateData['overseas_seniority'] = "无";
        }



        $this->resume->update($updateData);
             
    }


     public function curl($data){

        $curl = curl_init();
        $data = json_encode($data);


        curl_setopt_array($curl, array(
           CURLOPT_URL => 'https://api.chatanywhere.tech/v1/chat/completions',
           CURLOPT_RETURNTRANSFER => true,
           CURLOPT_ENCODING => '',
           CURLOPT_MAXREDIRS => 10,
           CURLOPT_TIMEOUT => 0,
           CURLOPT_FOLLOWLOCATION => true,
           CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
           CURLOPT_CUSTOMREQUEST => 'POST',
           CURLOPT_POSTFIELDS =>$data,
           CURLOPT_HTTPHEADER => array(
              'Authorization: Bearer '.env('CHATGPT_API_KEY'),
              'User-Agent: Apifox/1.0.0 (https://apifox.com)',
              'Content-Type: application/json'
           ),
        ));

       $response = curl_exec($curl);

       //dd($response);
       return $response;
    }
}
